﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Trippy.CollidableGameComponents;
using Trippy.CollidableGameComponents.Agents;
using Trippy.Levels;

namespace Trippy.Abilities
{
    //8 seconds of flight.
    class Fly : Ability
    {
        public Fly(Level level) : base(level)
        {
            name = "Fly";
            icon = CurrentLevel.Game.Content.Load<Texture2D>("Sprites/Ability Icons/fly");
        }
        public override void OnActivate()
        {
            if (CurrentLevel.Hero.State == Hero.HeroState.Grounded)
                CurrentLevel.Hero.Jump();
            if (State != AbilityState.Active)
            {
                State = AbilityState.Active;
                SetTimer(3);
            }
            base.OnActivate();
        }

        private void Float(float elapsedTime)
        {
            //slower rising but can still beat gravity while trying to go up when falling
            if (CurrentLevel.Hero.Velocity.Y >= -300)
                CurrentLevel.Hero.Velocity = new Vector2(CurrentLevel.Hero.Velocity.X, CurrentLevel.Hero.Velocity.Y - (1500.0f * elapsedTime));
            //else if (game.Hero.Velocity.Y >= -700)
                //game.Hero.Velocity = new Vector2(game.Hero.Velocity.X, game.Hero.Velocity.Y - (300.0f * elapsedTime));
        }

        public override void OnHold(float elapsedTime)
        {
            if (State == AbilityState.Active)
                Float(elapsedTime);
            base.OnHold(elapsedTime);
        }

        //faster horizontal air movement with fly buff
        public override void OnMoveLeft(float elapsedTime)
        {
            if (CurrentLevel.Hero.State == Hero.HeroState.Jumping)
                CurrentLevel.Hero.Velocity = new Vector2(CurrentLevel.Hero.Velocity.X - elapsedTime * 150.0f, CurrentLevel.Hero.Velocity.Y);
            base.OnMoveLeft(elapsedTime);
        }

        public override void OnMoveRight(float elapsedTime)
        {
            if (CurrentLevel.Hero.State == Hero.HeroState.Jumping)
                CurrentLevel.Hero.Velocity = new Vector2(CurrentLevel.Hero.Velocity.X + elapsedTime * 150.0f, CurrentLevel.Hero.Velocity.Y);
            base.OnMoveRight(elapsedTime);
        }

        public override void OnUpdate(float elapsedTime)
        {
            if (CurrentLevel.Hero.State == Hero.HeroState.Jumping)
            {
                if (((CurrentLevel.CurrentKeyboardState.IsKeyDown(CurrentLevel.Game.jump)
                && CurrentLevel.PreviousKeyboardState.IsKeyDown(CurrentLevel.Game.jump))) //this is disgusting as hell.
                && CurrentLevel.CurrentKeyboardState.IsKeyUp(CurrentLevel.Game.action)
                && State == AbilityState.Active)
                    Float(elapsedTime);
            }
            base.OnUpdate(elapsedTime);
        }

        public override void OnAir()
        {
            if (State == AbilityState.Available)
            {
                if (State != AbilityState.Active)
                {
                    State = AbilityState.Active;
                    SetTimer(3);
                }
            }
            base.OnAir();
        }

        public override void OnLand()
        {
            State = AbilityState.Available;
            KillTimer();
            base.OnLand();
        }
        protected override void OnTimer()
        {
            State = AbilityState.Unavailable;
            base.OnTimer();
        }
    }
}
